import '../Include.dart';

// 测试界面数据
class TestPage extends StatefulWidget {
  final String title;

  TestPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _TestPageState();
}

class _TestPageState extends BasePageState<TestPage> {
  String appBarTitle(BuildContext context) {
    return widget.title;
  }

  @override
  Widget buildBody(BuildContext context) {
    return new Center(
      child: FlatButton(
          onPressed: () {
            Application.router.navigateTo(
              context,
              Routes.TEST_ROUTE_NAME,
            );
          },
          child: Text(widget.title)),
    );
  }
}

class TestListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TestListPageState();
  }
}

class _TestListPageState extends BasePageState<TestListPage> {
  EmptyStatus emptyStatus = EmptyStatus.LOADING;
  LoadMoreStatus loadMoreStatus = LoadMoreStatus.LOADING;
  List<String> _list = List();
  EmptyStatus _emptyStatus = EmptyStatus.LOADING;
  LoadMoreStatus _loadMoreStatus = LoadMoreStatus.FINISH;
  int _pageSize = 8;

  @override
  void initState() {
    super.initState();
    test();
  }

  Future test() async {
//    await Future.delayed(Duration(seconds: 2), () {
////      setState(() {
////        emptyStatus = EmptyStatus.ERROR;
////        loadMoreStatus = LoadMoreStatus.ERROR;
////      });
////    });
    _getData(true, 1, _pageSize);
  }

  @override
  String appBarTitle(BuildContext context) {
    return '测试';
  }

//  @override
//  Widget buildBody(BuildContext context) {
//    return JvtdLoadMoreView(
//      loadMoreStatus: loadMoreStatus,
//      onErrorPressed: () {
//        setState(() {
//          loadMoreStatus = LoadMoreStatus.END;
//        });
//      },
//    );
//  }

//  @override
//  Widget buildBody(BuildContext context) {
//    return JvtdEmptyView(
//      emptyStatus: emptyStatus,
//      onErrorPressed: (){
//        setState(() {
//          emptyStatus = EmptyStatus.EMPTY;
//        });
//      },
//    );
//  }


  @override
  Widget buildBody(BuildContext context) {
    return JvtdListView<String>(
      listData: _list,
      headerList: <Widget>[
        Text('头布局'),
      ],
      footerList: <Widget>[
        Text('尾布局')
      ],
      itemWidgetBuild: (context, position) {
        return _buildItemWidget(context, position);
      },
      emptyConfig: EmptyConfig(emptyStatus: _emptyStatus,isHeaderAndFooter: true),
      refreshConfig: RefreshConfig(refreshCallback: _refresh),
      loadMoreConfig: LoadMoreConfig(
          loadMoreStatus: _loadMoreStatus,
          loadMoreCallback: (pageNum, pageSize) {
            _getData(false, pageNum, pageSize);
          }),
    );
  }

  Future<Null> _refresh() async{
    _getData(true, 1, _pageSize);
  }

  void _getData(bool isRefresh, int num, int size) {
    setState(() {
      if(!isRefresh){
        _loadMoreStatus = LoadMoreStatus.LOADING;
      }
    });
    Future.delayed(Duration(seconds: 2), () {
      int num1 = Random.secure().nextInt(100);
      if (num1 < 10) {
        //测试获取失败
        print("获取失败");
        setState(() {
          if (isRefresh) {
            _list = List();
            _emptyStatus = EmptyStatus.ERROR;
            print("刷新获取失败");
          } else {
            _loadMoreStatus = LoadMoreStatus.ERROR;
            print("加载获取失败");
          }
        });
      } else if (num1 > 10 && num1 < 40) {
        //测试正常
        print("获取正常");
        int size1 = Random.secure().nextInt(100);
        if (size1 > 50) {
          size1 = Random.secure().nextInt(size1);
        }

        setState(() {
          if(isRefresh) {
            _list = List();
          }
          _list.addAll(List.generate(size, (i) => '我是第 $num 次数据 $i'));
          if (size1 < _pageSize) {
            _loadMoreStatus = LoadMoreStatus.END;
          } else {
            _loadMoreStatus = LoadMoreStatus.FINISH;
          }
        });
      } else {
        //正常为空
        print("获取空");
        setState(() {
          if (isRefresh) {
            _list = List();
            _emptyStatus = EmptyStatus.EMPTY;
          } else {
            _loadMoreStatus = LoadMoreStatus.END;
          }
        });
      }
    });
  }

  Widget _buildItemWidget(BuildContext context, int position) {
    return Center(
      heightFactor: 1.1,
      child: FlatButton(
          onPressed: () {
            JvtdToastUtils.showMessage(msg: _list[position]);
          },
          child: Text(
            _list[position],
          )),
    );
  }
}
